package com.boruishangge.persagysdk.network;


import android.text.TextUtils;

import com.boruishangge.persagysdk.base.AppApplicationContext;
import com.boruishangge.persagysdk.base.BaseActivity;
import com.boruishangge.persagysdk.entiy.BaseModel;
import com.boruishangge.persagysdk.uitls.EmptyViewUtils.ShowExceptionUIHelper;
import com.boruishangge.persagysdk.uitls.NetUtil;
import com.boruishangge.persagysdk.uitls.StringUtils;
import com.google.gson.JsonParseException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

public abstract class BaseObserver<T> implements Observer<BaseModel<T>> {

    private BaseActivity mActivity;

    public BaseObserver(BaseActivity mActivity) {
        this.mActivity = mActivity;
    }

    @Override
    public void onSubscribe(Disposable d) {

    }
        @Override
    public void onNext(@NonNull BaseModel<T> value) {
        String code=value.getRespCode();
        if (!StringUtils.checkNull(code)) {
            switch (code){
                case HttpCode.APLUS_success:
                    T t = value.getData();
                    onSuccess(t);
                    if (t == null) {
                        returnResultMsgOrReason(value.getMsg());
                    }
                    break;
                case HttpCode.APLUS_failure:
                    onFailure("服务繁忙！请稍后再试");
                    break;
                default:
                    onFailure(value.getMsg());
                    contentForFailure(value.getData());
                    break;

            }
        } else {
            onFailure("服务繁忙！请稍后再试");
        }
    }



    @Override
    public void onError(@NonNull Throwable e) {
        //mActivity.dismiss();
        if (NetUtil.getNetWorkState(AppApplicationContext.context) < 0) {
            mActivity.showErrorWithStatus("请检查网络设置");
        } else if (e instanceof HttpException) {
            mActivity.showErrorWithStatus("请求失败");
        } else if (e instanceof JsonParseException) {
            mActivity.showErrorWithStatus("数据错误");
        } else if (e instanceof ConnectException) {
            mActivity.showErrorWithStatus("连接失败");
        } else if (e instanceof SocketTimeoutException) {
            mActivity.showErrorWithStatus("网络连接超时");
        } else {
            mActivity.showErrorWithStatus("未知错误");
        }
        if (null != mActivity.mShowExceptionUIHelper) {
            mActivity.mShowExceptionUIHelper.refreshUI(ShowExceptionUIHelper.NETWORK_ERROR);
        }
        e.printStackTrace();
    }

    @Override
    public void onComplete() {

    }
    /**
     * 接口请求成功且当接口字段 result或ResultMsg 返回为true时调用
     * 考虑到登录或连续发多个网络请求的特殊界面，弹框消失没调用；手动成功后调用即可
     *
     * @param t
     */
    protected abstract void onSuccess(T t);

    /**
     * 将ResultMsg或者Reason在接口成功的情况下返回，按需使用
     *
     * @param str
     */
    protected void returnResultMsgOrReason(String str) {

    }

    /**
     * 将getData内容在接口失败的情况下返回，按需使用
     *
     * @param t
     */
    protected void contentForFailure(T t) {

    }

    /**
     * 接口请求成功且当接口字段 result或ResultMsg 返回为false时调用
     * 以下是默认处理逻辑，如果某个界面单独处理则需要重写此方法，并省去重写方法里的super.onFailure(msg);
     *
     * @param msg
     */
    protected void onFailure(String msg) {
        if (TextUtils.isEmpty(msg)) {
            mActivity.showErrorWithStatus("请求失败");
            return;
        }
        if (msg.contains("Exception")) {
            mActivity.showErrorWithStatus("请求失败");
        } else {
            mActivity.showErrorWithStatus(msg);
        }
    }
}
